package q103_zigzagLevelOrder;

import CommonClass.Common.TreeNode;

import java.util.*;

public class Solution_1 {

    public static void main(String[] args) {

    }
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        boolean mode = false;
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            int currentSize = queue.size();

            for (int i = 0; i < currentSize; i++) {
                TreeNode bin = queue.poll();
                level.add(bin.val);
                if (bin.left != null) {
                    queue.offer(bin.left);
                }
                if (bin.right != null) {
                    queue.offer(bin.right);
                }
            }
            // 只需要隔行进行reverse即可
            if(mode){
                Collections.reverse(level);
            }
            mode = !mode;
            res.add(level);
        }

        return res;
    }
}
